/**
 * 
 */
package com.mc.test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.io.file.FileReader;

/**
 * @author w.weizhen
 *
 */
public class ImgStr {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		String user_dir = System.getProperty("user.dir") + File.separator;
		String data_path = user_dir + "data" + File.separator +"notice.txt";
		FileReader fileReader = new FileReader(data_path);
		String result = fileReader.readString();
		List<String> imgs = getImgStr(result);
		System.out.println(imgs.toString());
//		for (int i = 0; i < imgs.size(); i++) 
//		{
//			String img = imgs.get(i);
//			System.out.println(img);
//			System.out.println(img.substring(img.indexOf("hyfs") + "hyfs".length(), img.length()));
//			System.out.println("");
//		}
	}
	
	/**
	 * 得到网页中图片的地址
	 * 
	 * @param sets html字符串
	 */
	private static List<String> getImgStr(String htmlStr) 
    {
        List<String> pics = new ArrayList<String>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String url = m.group(1);
                if(StringUtils.isNotBlank(url))
                {
                    String questionMark = "?";
                    if(url.indexOf(questionMark) > -1)
                    {
                        url = url.substring(0, url.indexOf(questionMark));
                    }
                    pics.add(url);
                }
            }
        }
        return pics;
    }
}